package org.seckill.aspect;

import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.seckill.conf.DynamicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/31 0031.
 */
//@Configuration
//@MapperScan(value = {"com.zhangshuo.test1.mapper", "com.zhangshuo.test2.mapper"})
public class DataSourceConfig {
  /*  @ConfigurationProperties(prefix = "datasource1")
    @Bean(name = "datasource1")
// @Primary
*//**
 * 在方法上注解configurationProperties时，将会把属性注入到返回结果的bean中
 *//*
    public DruidDataSource dataSource1() throws SQLException {
        return new DruidDataSource();
    }

    @ConfigurationProperties(prefix = "datasource2")
    @Bean(name = "datasource2")
*//**
 * 在方法上注解configurationProperties时，将会把属性注入到返回结果的bean中
 *//*
    public DruidDataSource dataSource2()throws SQLException {
        return new DruidDataSource();
    }

    @Bean(name = "datasource")
    @Primary
    public DynamicDataSource dynamicDataSource(@Qualifier(value = "datasource1") DataSource dataSource1, @Qualifier(value = "datasource2") DataSource dataSource2) {
        DynamicDataSource bean = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("datasource1", dataSource1);
        targetDataSources.put("datasource2", dataSource2);
        bean.setTargetDataSources(targetDataSources);
        bean.setDefaultTargetDataSource(dataSource1);
        return bean;
    }

    @Bean(name = "sessionFactory1")
    @ConfigurationProperties(prefix = "mybatis-plus1")
    @ConfigurationPropertiesBinding()
    @Primary
    public MybatisSqlSessionFactoryBean sqlSessionFactory1(@Qualifier(value = "datasource") DataSource dataSource) {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }

    @Bean(name = "sessionFactory2")
    @ConfigurationProperties(prefix = "mybatis-plus2")
    @ConfigurationPropertiesBinding()
    public MybatisSqlSessionFactoryBean sqlSessionFactory2(@Qualifier(value = "datasource") DataSource dataSource) {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }*/
}